/*****************************************************************************************
 * Aware : an Asynchonous Aware-Oriented Software Architecture
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *****************************************************************************************/
package dexter.aware.policy;

import java.util.Hashtable;

/**
 * StatePolicy class
 * 
 * @author Ruben Mondejar <rmondejara@uoc.edu>
 */
public class StatePolicy implements SelectorPolicy<int[]> {

	private int[] thresholds;
	
	public StatePolicy(int[] thresholds) {
	   this.thresholds = thresholds;	
	}

	@Override
	public Hashtable<String, int[]> filter(
			Hashtable<String, int[]> hosts) {
		
		Hashtable<String, int[]> filtered = new Hashtable<String, int[]>();
		for (String host : hosts.keySet()) {
			int[] state = hosts.get(host);
			if (isOver(state)) {
				filtered.put(host, state);
			}
		}
		return filtered;
	}

	private boolean isOver(int[] state) {
		boolean over = true;
		for (int i=0; over && (i<state.length);i++) {
		  over &= state[i] >= thresholds[i];	
		}
		return over;
	}
	
	public String toString() {
		return "StatePolicy :cpu% > "+thresholds[0]+", mem% > "+thresholds[1]+", disk% > "+thresholds[2]+"";
	}
}
